package com.example.wechat.service.impl;



import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.educate.exception.EducateException;
import com.example.educate.model.wechat.Menu;
import com.example.educate.vo.wechat.MenuVo;
import com.example.wechat.mapper.MenuMapper;
import com.example.wechat.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.service.WxService;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单明细 订单明细 服务实现类
 * </p>
 *
 * @author Xiao
 * @since 2022-09-18
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private WxMpService wxMpService;

    /**
     * 说明：
     * 自定义菜单最多包括3个一级菜单，每个一级菜单最多包含5个二级菜单。
     * 一级菜单最多4个汉字，二级菜单最多8个汉字，多出来的部分将会以“...”代替。
     * 创建自定义菜单后，菜单的刷新策略是，在用户进入公众号会话页或公众号profile页时，如果发现上一次拉取菜单的请求在5分钟以前，就会拉取一下菜单，如果菜单有更新，就会刷新客户端的菜单。测试时可以尝试取消关注公众账号后再次关注，则可以看到创建后的效果。
     */
    @SneakyThrows
    @Override
    public void syncMenu() {
        //字符换型，换成指定的字符串型
        JSONArray firstMenuArray = new JSONArray();
        List<MenuVo> menuInfo = this.findMenuInfo();
//        {
//            "button":[
//            {
//                "type":"click",
//                    "name":"今日歌曲",
//                    "key":"V1001_TODAY_MUSIC"
//            },
        for (MenuVo menuVo : menuInfo) {
            JSONObject firstMenuObject = new JSONObject();
            firstMenuObject.put("type",menuVo.getType());
            firstMenuObject.put("name",menuVo.getName());
            firstMenuObject.put("key",menuVo.getMeunKey());
            JSONArray seconedMenuArray = new JSONArray();
//            {
//           "name":"菜单",
//           "sub_button":[
//           {
//               "type":"view",
//               "name":"搜索",
//               "url":"http://www.soso.com/"
//            },
            for (MenuVo child : menuVo.getChildren()) {
                JSONObject seconedMenuObject = new JSONObject();
                seconedMenuObject.put("name",child.getName());
                seconedMenuObject.put("type",child.getType());
                if ("view".equals(child.getType())) {
                         //  ***这个地方后期可能需要进行更改
                    seconedMenuObject.put("url","http://ggkt2.vipgz1.91tunnel.com/#"+child.getUrl());
                }else {
                    seconedMenuObject.put("key",child.getMeunKey());
                }
                seconedMenuArray.add(seconedMenuObject);
            }
            firstMenuObject.put("sub_button",seconedMenuArray);
            firstMenuArray.add(firstMenuObject);
        }
        JSONObject buttonModal = new JSONObject();
        buttonModal.put("button",firstMenuArray);
//        System.out.println(buttonModal.toJSONString());

        try {
            String menuId =
                    this.wxMpService.getMenuService().menuCreate(buttonModal.toJSONString());
            System.out.println("menuId"+menuId);
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new EducateException(20001,"公众号菜单同步失败");
        }
    }

    @Override
    public void removeMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new EducateException(2001,"公众号菜单删除失败");
        }
    }


    @Override
    public List<Menu> findOneMenuInfo() {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        return baseMapper.selectList(wrapper.eq(Menu::getParentId, 0));
    }

    @Override
    public List<MenuVo> findMenuInfo() {
        List<MenuVo> finalMenuList = new ArrayList<>();
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        List<Menu> menuOneList = baseMapper.selectList(wrapper.eq(Menu::getParentId, 0));
        for (Menu menu : menuOneList) {
            LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
            MenuVo menuVo = new MenuVo();
            List<MenuVo> children = new ArrayList<>();
            BeanUtils.copyProperties(menu,menuVo);
            List<Menu> menuTwoList = baseMapper.selectList(queryWrapper.eq(Menu::getParentId,menu.getId()));
            for (Menu menu2 : menuTwoList) {
                MenuVo menuVo2 = new MenuVo();
                BeanUtils.copyProperties(menu2,menuVo2);
                children.add(menuVo2);
            }
            menuVo.setChildren(children);
            finalMenuList.add(menuVo);
        }
        return finalMenuList;
    }

}
